package vip.web3.admin.sys.controller.biz;

import com.alibaba.excel.EasyExcel;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import vip.web3.admin.sys.controller.base.AbstractController;
import vip.web3.admin.sys.entity.biz.ARecordEntity;
import vip.web3.admin.sys.form.user.RecordEditForm;
import vip.web3.admin.sys.form.user.RecordPageForm;
import vip.web3.admin.sys.service.biz.IARecordService;
import vip.web3.admin.sys.vo.biz.export.*;
import vip.web3.common.utils.DateUtils;
import vip.web3.common.utils.PageUtils;
import vip.web3.common.utils.R;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 分红明细Controller
 * <p>
 * aut-horlilei
 *
 * @date 2024-09-27
 */
@RestController
@RequestMapping("/sys/record")
public class ARecordController extends AbstractController {

    @Autowired
    private IARecordService aRecordService;


    /**
     * 查询分红明细列表
     */
    @RequiresPermissions("sys:record:list")
    @GetMapping("/list")
    public R list(@RequestParam Map<String, Object> params, @Valid RecordPageForm form) {
        PageUtils page = aRecordService.pageRecords(params, form);
        return R.ok().put("page", page);
    }

    @GetMapping("/exRecord")
    public void exRecord(HttpServletResponse response, @RequestParam Map<String, Object> params, @Valid RecordPageForm form) throws IOException {
        List<ExRecordVo> list = aRecordService.exRecord(params, form);
        if (CollectionUtils.isEmpty(list)) {
            list = Collections.EMPTY_LIST;
        }
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("流水数据_" + DateUtils.format(new Date(), DateUtils.DATE_TIME_PATTERN), "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), ExRecordVo.class).sheet("流水").doWrite(list);
    }

    @GetMapping("/exReCny")
    public void exReCny(HttpServletResponse response, @RequestParam Map<String, Object> params, @Valid RecordPageForm form) throws IOException {
        List<ExReCnyVo> list = aRecordService.exReCny(params, form);
        if (CollectionUtils.isEmpty(list)) {
            list = Collections.EMPTY_LIST;
        }
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("CNY充值数据_" + DateUtils.format(new Date(), DateUtils.DATE_TIME_PATTERN), "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), ExReCnyVo.class).sheet("CNY充值").doWrite(list);
    }

    @GetMapping("/exReUsdt")
    public void exReUsdt(HttpServletResponse response, @RequestParam Map<String, Object> params, @Valid RecordPageForm form) throws IOException {
        List<ExReUsdtVo> list = aRecordService.exReUsdt(params, form);
        if (CollectionUtils.isEmpty(list)) {
            list = Collections.EMPTY_LIST;
        }
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("USDT充值数据_" + DateUtils.format(new Date(), DateUtils.DATE_TIME_PATTERN), "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), ExReUsdtVo.class).sheet("USDT充值").doWrite(list);
    }

    @GetMapping("/exCny")
    public void exCny(HttpServletResponse response, @RequestParam Map<String, Object> params, @Valid RecordPageForm form) throws IOException {
        List<ExCnyVo> list = aRecordService.exCny(params, form);
        if (CollectionUtils.isEmpty(list)) {
            list = Collections.EMPTY_LIST;
        }
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("CNY提现数据_" + DateUtils.format(new Date(), DateUtils.DATE_TIME_PATTERN), "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), ExCnyVo.class).sheet("CNY提现").doWrite(list);
    }

    @GetMapping("/exUsdt")
    public void exUsdt(HttpServletResponse response, @RequestParam Map<String, Object> params, @Valid RecordPageForm form) throws IOException {
        List<ExUsdtVo> list = aRecordService.exUsdt(params, form);
        if (CollectionUtils.isEmpty(list)) {
            list = Collections.EMPTY_LIST;
        }
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("USDT提现数据_" + DateUtils.format(new Date(), DateUtils.DATE_TIME_PATTERN), "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), ExUsdtVo.class).sheet("USDT提现").doWrite(list);
    }

    /**
     * 新增保存分红明细
     */
    @RequiresPermissions("sys:record:add")
    @PostMapping("/add")
    public R addSave(@Valid @RequestBody ARecordEntity aRecord) {
        aRecordService.save(aRecord);
        return R.ok();
    }

    /**
     * 修改分红明细
     */
    @RequiresPermissions("sys:record:edit")
    @GetMapping("/edit/{id}")
    public R edit(@PathVariable("id") Integer id) {
        ARecordEntity aRecord = aRecordService.getById(id);
        return R.ok().put("data", aRecord);
    }

    /**
     * 修改保存分红明细
     */
//    @RequiresPermissions("sys:record:edit")
    @PostMapping("/edit")
    public R editSave(@Valid @RequestBody RecordEditForm form) {

        aRecordService.edit(form);
        return R.ok();
    }

    /**
     * 删除分红明细
     */
    @RequiresPermissions("sys:record:remove")
    @PostMapping("/remove")
    public R remove(String ids) {
        int i = aRecordService.deleteARecordByIds(ids);
        return R.ok();
    }
}
